//-----------------Sonic CD DrillBot Script-------------------//
// Yeah...

// Aliases
#alias Object.Value0		:	Object.Timer
#alias Object.Value1		:	Object.DrillTimer
#alias Object.Value2		:	Object.JumpBlock
#alias Object.Frame			:	Object.WalkTimer
#alias Object.Value3		:	Object.XVelocity
#alias Object.Value4		:	Object.YVelocity
#alias Object.Value5		:	Object.XOriginPos
#alias Object.Value6		:	Object.YOriginPos

#alias Object.PropertyValue	:	Object.Quality


// States
#alias 0	:	DRILLBOT_IDLE
#alias 1	:	DRILLBOT_JUMPLEFT
#alias 2	:	DRILLBOT_JUMPRIGHT
#alias 3	:	DRILLBOT_WALKLEFT
#alias 4	:	DRILLBOT_WALKRIGHT
#alias 5	:	DRILLBOT_TURN

// Collision Sides
#alias 0	:	CSIDE_FLOOR
#alias 1	:	CSIDE_LWALL
#alias 2	:	CSIDE_RWALL

// Badnik Quality / Property Values
#alias 0	:	GOOD_QUALITY
#alias 1	:	BAD_QUALITY

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

sub ObjectMain
	CallFunction(StageSetup_CheckGoodFuture)	// Check if this should be a flower
	if Object.Quality == GOOD_QUALITY
		switch Object.State
			case DRILLBOT_IDLE
				Object.YVelocity = -0x50000
					Object.XVelocity = -0x8000
				break
			
			case DRILLBOT_JUMPLEFT
				ObjectTileCollision(CSIDE_RWALL, -16, 0, 0)
				if CheckResult == false
					Object.XPos += Object.XVelocity
				end if
				Object.YVelocity += 0x2000
				Object.YPos += Object.YVelocity
				if Object.YVelocity > 0
					Object.State = DRILLBOT_WALKLEFT
				end if
				break
				
			case DRILLBOT_JUMPRIGHT
				ObjectTileCollision(CSIDE_LWALL, 16, 0, 0)
					if CheckResult == false
						Object.XPos -= Object.XVelocity
					end if
				Object.YPos += Object.YVelocity
				Object.YVelocity += 0x2000
				if Object.YVelocity > 0
					Object.State = DRILLBOT_WALKRIGHT
				end if
				break
			
			case DRILLBOT_WALKLEFT
					ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
					if CheckResult == false
						Object.YVelocity += 0x1500
						Object.YPos += Object.YVelocity
					end if
					if CheckResult == true
						Object.JumpBlock = 0
					end if
				Object.Direction = FACING_RIGHT
					ObjectTileCollision(CSIDE_RWALL, -16, 0, 0)
					if CheckResult == false
						Object.XPos -= 0x10000
					else
						if Object.JumpBlock == 0
							Object.JumpBlock = 1
							Object.State = DRILLBOT_JUMPLEFT
							Object.YVelocity = -0x50000
							Object.XVelocity = -0x8000
						end if
					end if
				ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
				if CheckResult == true
					if Player.XPos > Object.XPos
						Object.State = DRILLBOT_TURN
					end if 
				end if
				break
			
			case DRILLBOT_TURN
				Object.Timer++
				if Object.Timer == 30
				if Object.Direction == FACING_RIGHT
					Object.Direction = FACING_LEFT
					Object.State = DRILLBOT_WALKRIGHT
				else
					Object.Direction = FACING_RIGHT
					Object.State = DRILLBOT_WALKLEFT
				end if
				Object.Timer = 0
				end if
				break
			
			case DRILLBOT_WALKRIGHT
					ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
					if CheckResult == false
						Object.YVelocity += 0x1500
						Object.YPos += Object.YVelocity
					end if
					if CheckResult == true
						Object.JumpBlock = 0
					end if
				Object.Direction = FACING_LEFT
					ObjectTileCollision(CSIDE_LWALL, 16, 0, 0)
					if CheckResult == false
						Object.XPos += 0x10000
					else
						if Object.JumpBlock == 0
							Object.JumpBlock = 1
							Object.State = DRILLBOT_JUMPRIGHT
							Object.YVelocity = -0x50000
							Object.XVelocity = -0x8000
						end if
					end if
						
				ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
				if CheckResult == true
					if Player.XPos < Object.XPos
						Object.State = DRILLBOT_TURN
					end if 
				end if
				break
		end switch
	else // Bad Quality
		switch Object.State
			case DRILLBOT_IDLE
				Object.YVelocity = -0x50000
				Object.XVelocity = -0x8000
				break
			
			case DRILLBOT_JUMPLEFT
				ObjectTileCollision(CSIDE_RWALL, -16, 0, 0)
				if CheckResult == false
					Object.XPos += Object.XVelocity
				end if
				Object.YVelocity += 0x2000
				Object.YPos += Object.YVelocity
				if Object.YVelocity > 0
					Object.State = DRILLBOT_WALKLEFT
				end if
				break
				
			case DRILLBOT_JUMPRIGHT
				ObjectTileCollision(CSIDE_LWALL, 16, 0, 0)
					if CheckResult == false
						Object.XPos -= Object.XVelocity
					end if
				Object.YPos += Object.YVelocity
				Object.YVelocity += 0x2000
				if Object.YVelocity > 0
					Object.State = DRILLBOT_WALKRIGHT
				end if
				break
			
			case DRILLBOT_WALKLEFT
				Object.DrawOrder = 3
					ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
					if CheckResult == false
						Object.YVelocity += 0x1500
						Object.YPos += Object.YVelocity
					end if
					if CheckResult == true
						Object.JumpBlock = 0
					end if
				Object.Direction = FACING_RIGHT
					ObjectTileCollision(CSIDE_RWALL, -16, 0, 0)
					if CheckResult == false
						Object.XPos -= 0x5000
					else
						if Object.JumpBlock == 0
							Object.JumpBlock = 1
							Object.State = DRILLBOT_JUMPLEFT
							Object.YVelocity = -0x30000
							Object.XVelocity = -0x8000
						end if
					end if
				ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
				if CheckResult == true
					if Player.XPos > Object.XPos
						Object.State = DRILLBOT_TURN
					end if 
				end if
				break
			
			case DRILLBOT_TURN
				Object.Timer++
				if Object.Timer == 60
				if Object.Direction == FACING_RIGHT
					Object.Direction = FACING_LEFT
					Object.State = DRILLBOT_WALKRIGHT
				else
					Object.Direction = FACING_RIGHT
					Object.State = DRILLBOT_WALKLEFT
				end if
				Object.Timer = 0
				end if
				break
			
			case DRILLBOT_WALKRIGHT
				Object.DrawOrder = 3
					ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
					if CheckResult == false
						Object.YVelocity += 0x1500
						Object.YPos += Object.YVelocity
					end if
					if CheckResult == true
						Object.JumpBlock = 0
					end if
				Object.Direction = FACING_LEFT
					ObjectTileCollision(CSIDE_LWALL, 16, 0, 0)
					if CheckResult == false
						Object.XPos += 0x5000
					else
						if Object.JumpBlock == 0
							Object.JumpBlock = 1
							Object.State = DRILLBOT_JUMPRIGHT
							Object.YVelocity = -0x30000
							Object.XVelocity = -0x8000
						end if
					end if
						
				ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
				if CheckResult == true
					if Player.XPos < Object.XPos
						Object.State = DRILLBOT_TURN
					end if 
				end if
				break
		end switch
	end if
	
	// Reset object when out of bounds
	if Object.OutOfBounds == true
		Object.Priority = PRIORITY_BOUNDS
		Object.State = DRILLBOT_IDLE
		Object.Timer = 0
		Object.XPos = Object.XOriginPos
		Object.YPos = Object.YOriginPos
	end if	
	CallFunction(StageSetup_CheckGoodFuture)	// Check if it should be a flower
end sub


sub ObjectPlayerInteraction
		switch Object.State
			case DRILLBOT_IDLE
				PlayerObjectCollision(C_TOUCH, -128, -148, 0, -20) // Left Detect
				if CheckResult == true
					Object.State = DRILLBOT_JUMPLEFT
					PlayStageSfx(0, false)
				end if
				PlayerObjectCollision(C_TOUCH, 0, -148, 128, -20)// Right Detect
				if CheckResult == true
					Object.State = DRILLBOT_JUMPRIGHT
					Object.Direction = FACING_LEFT
					PlayStageSfx(0, false)
				end if
				PlayerObjectCollision(C_TOUCH, -12, -20, 12, 20)//Drill Bot
				if CheckResult == true
					CallFunction(Player_BadnikBreak)
				end if
				PlayerObjectCollision(C_TOUCH, -8, -36, 8, -20)// Drill Touch
				if CheckResult == true
					CallFunction(Player_Hit)
				end if
				break
				
			case DRILLBOT_JUMPLEFT
			case DRILLBOT_JUMPRIGHT
				PlayerObjectCollision(C_TOUCH, -12, -20, 12, 20)//Drill Bot
				if CheckResult == true
					CallFunction(Player_BadnikBreak)
				end if
				PlayerObjectCollision(C_TOUCH, -8, -36, 8, -20)// Drill Touch
				if CheckResult == true
					CallFunction(Player_Hit)
				end if
				break
				
			case DRILLBOT_WALKLEFT
			case DRILLBOT_TURN
				PlayerObjectCollision(C_TOUCH, -16, -20, 16, 20)
				if CheckResult == true
					CallFunction(Player_BadnikBreak)
				end if
				TempValue0 = Object.WalkTimer
				TempValue0 /= 10
				if Object.WalkTimer > 39
					Object.WalkTimer = 0
					TempValue0 = 0
				end if
				switch TempValue0
					case 0
						PlayerObjectCollision(C_TOUCH, -22, -7, 4, 8)
						break
					case 1
						PlayerObjectCollision(C_TOUCH, -19, -8, 4, 8)
						break
					case 2
						PlayerObjectCollision(C_TOUCH, -15, -7, 4, 8)
						break
					case 3
						PlayerObjectCollision(C_TOUCH, -22, -7, 4, 8)
						break
				end switch
				if CheckResult == true
					CallFunction(Player_Hit)
				end if
				break
				
			case DRILLBOT_WALKRIGHT
				PlayerObjectCollision(C_TOUCH, -16, -20, 16, 20)
				if CheckResult == true
					CallFunction(Player_BadnikBreak)
				end if
				TempValue0 = Object.WalkTimer
				TempValue0 /= 10
				if Object.WalkTimer > 39
					Object.WalkTimer = 0
					TempValue0 = 0
				end if
				switch TempValue0
					case 0
						PlayerObjectCollision(C_TOUCH, -4, -7, 22, 8)
						break
					case 1
						PlayerObjectCollision(C_TOUCH, -4, -8, 19, 8)
						break
					case 2
						PlayerObjectCollision(C_TOUCH, -4, -7, 15, 8)
						break
					case 3
						PlayerObjectCollision(C_TOUCH, -4, -7, 22, 8)
						break
				end switch
				if CheckResult == true
					CallFunction(Player_Hit)
				end if
				break
		end switch
end sub


sub ObjectDraw
	Object.DrillTimer++
	if Object.Quality == GOOD_QUALITY
		if Object.DrillTimer <= 4
			TempValue0 = 0
		end if
		if Object.DrillTimer > 4
			TempValue0 = 1
		end if
		if Object.DrillTimer > 8
			Object.DrillTimer = 0
		end if
	end if
	if Object.Quality == BAD_QUALITY
		if Object.DrillTimer <= 8
			TempValue0 = 0
		end if
		if Object.DrillTimer > 8
			TempValue0 = 1
		end if
		if Object.DrillTimer > 16
			Object.DrillTimer = 0
		end if
	end if
	if Object.Quality == GOOD_QUALITY
		switch Object.State
			case DRILLBOT_IDLE
			case DRILLBOT_JUMPLEFT
			case DRILLBOT_JUMPRIGHT
				DrawSpriteFX(TempValue0, FX_FLIP, Object.XPos, Object.YPos)// Drill
				DrawSpriteFX(2, FX_FLIP, Object.XPos, Object.YPos)// Drill Bot
				break
			
			case DRILLBOT_WALKLEFT
			case DRILLBOT_WALKRIGHT
			case DRILLBOT_TURN
				if Object.State == DRILLBOT_TURN
				else
					Object.WalkTimer++
				end if
				TempValue0 = Object.WalkTimer	// Drill
				TempValue1 = Object.WalkTimer	// Drill Bot
				TempValue2 = Object.WalkTimer	// Drill Base
				TempValue0 /= 10	// Drill
				TempValue1 /= 10	// Drill Bot
				TempValue2 /= 10	// Drill Base
				if Object.DrillTimer <= 4
					TempValue0 += 3		// Drill
				end if
				if Object.DrillTimer > 4
					TempValue0 += 8		// Drill
				end if
				TempValue1 += 18	// Drill Bot
				TempValue2 += 13	// Drill Base
				if Object.WalkTimer > 39
					Object.WalkTimer = 0
					if Object.DrillTimer <= 4
						TempValue0 = 3		// Drill
					end if
					if Object.DrillTimer > 4
						TempValue0 = 7		// Drill
					end if
					TempValue1 = 18		// Drill Bot
					TempValue2 = 13		// Drill Base
				end if
				DrawSpriteFX(TempValue1, FX_FLIP, Object.XPos, Object.YPos)// Drill Bot
				DrawSpriteFX(TempValue0, FX_FLIP, Object.XPos, Object.YPos) // Drill
				DrawSpriteFX(TempValue2, FX_FLIP, Object.XPos, Object.YPos)// Drill Base
			break
		end switch
	else // Bad Quality
		switch Object.State
			case DRILLBOT_IDLE
			case DRILLBOT_JUMPLEFT
			case DRILLBOT_JUMPRIGHT
				TempValue0 += 23
				DrawSpriteFX(TempValue0, FX_FLIP, Object.XPos, Object.YPos)// Drill
				DrawSpriteFX(25, FX_FLIP, Object.XPos, Object.YPos)// Drill Bot
				break
			
			case DRILLBOT_WALKLEFT
			case DRILLBOT_WALKRIGHT
			case DRILLBOT_TURN
				if Object.State == DRILLBOT_TURN
				else
				Object.WalkTimer++
				end if
				TempValue0 = Object.WalkTimer
				TempValue1 = Object.WalkTimer
				TempValue2 = Object.WalkTimer
				TempValue0 /= 10
				TempValue1 /= 10
				TempValue2 /= 10
				if Object.DrillTimer <= 4
					TempValue0 += 26
				end if
				if Object.DrillTimer > 4
					TempValue0 += 31
				end if
				TempValue1 += 41
				TempValue2 += 36
				TempValue3 = Object.YPos
				if Object.WalkTimer > 39
					Object.WalkTimer = 0
					if Object.DrillTimer <= 4
						TempValue0 = 26
					end if
					if Object.DrillTimer > 4
						TempValue0 = 31
					end if
					TempValue1 = 41
					TempValue2 = 36
				end if
				DrawSpriteFX(TempValue1, FX_FLIP, Object.XPos, Object.YPos)// Drill Bot
				DrawSpriteFX(TempValue0, FX_FLIP, Object.XPos, Object.YPos) // Drill
				DrawSpriteFX(TempValue2, FX_FLIP, Object.XPos, Object.YPos)// Drill Base
			break
		end switch
	end if
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects.gif")

	// Good Quality
	SpriteFrame(-8, -36, 16, 16, 26, 1)			// #0 - Drill Frame 1
	SpriteFrame(-8, -36, 16, 16, 43, 1)			// #1 - Drill Frame 2
	SpriteFrame(-12, -20, 24, 40, 1, 1)			// #2 - IDLE
	//Walk
	SpriteFrame(-22, -7, 16, 16, 159, 1)		// #3 - Drill Frame 1(Walk Frame 1)
	SpriteFrame(-19, -8, 16, 16, 159, 1)		// #4 - Drill Frame 1(Walk Frame 2)
	SpriteFrame(-15, -7, 16, 16, 159, 1)		// #5 - Drill Frame 1(Walk Frame 3)
	SpriteFrame(-19, -8, 16, 16, 159, 1)		// #6 - Drill Frame 1(Walk Frame 4)
	SpriteFrame(-19, -8, 16, 16, 159, 1)		// #7 - Drill Frame 1(Walk Frame 4Dub)
	SpriteFrame(-22, -7, 16, 16, 176, 1)		// #8 - Drill Frame 2(Walk Frame 1)
	SpriteFrame(-19, -8, 16, 16, 176, 1)		// #9 - Drill Frame 2(Walk Frame 2)
	SpriteFrame(-15, -7, 16, 16, 176, 1)		// #10 - Drill Frame 2(Walk Frame 3)
	SpriteFrame(-19, -8, 16, 16, 176, 1)		// #11 - Drill Frame 2(Walk Frame 4)
	SpriteFrame(-19, -8, 16, 16, 176, 1)		// #12 - Drill Frame 2(Walk Frame 4Dub)
	SpriteFrame(-6, -7, 16, 16, 193, 1)			// #13 - Drill Base(Walk Frame 1)
	SpriteFrame(-3, -8, 16, 16, 193, 1)			// #14 - Drill Base(Walk Frame 2)
	SpriteFrame(1, -7, 16, 16, 193, 1)			// #15 - Drill Base(Walk Frame 3)
	SpriteFrame(-3, -8, 16, 16, 193, 1)			// #16 - Drill Base(Walk Frame 4)
	SpriteFrame(-3, -8, 16, 16, 193, 1)			// #17 - Drill Base(Walk Frame 4Dub)
	SpriteFrame(-16, -20, 32, 40, 60, 1)		// #18 - Walk Frame 1
	SpriteFrame(-16, -20, 32, 40, 93, 1)		// #19 - Walk Frame 2
	SpriteFrame(-16, -20, 32, 40, 126, 1)		// #20 - Walk Frame 3
	SpriteFrame(-16, -20, 32, 40, 93, 1)		// #21 - Walk Frame 4
	SpriteFrame(-16, -20, 32, 40, 93, 1)		// #22 - Walk Frame 4Dub
	
	
	// Bad Quality
	SpriteFrame(-8, -36, 16, 16, 26, 18)		// #23 - Drill Frame 1
	SpriteFrame(-8, -36, 16, 16, 43, 18)		// #24 - Drill Frame 2
	SpriteFrame(-12, -20, 24, 40, 1, 42)		// #25 - IDLE
	//Walk
	SpriteFrame(-22, -7, 16, 16, 159, 18)		// #26 - Drill Frame 1(Walk Frame 1)
	SpriteFrame(-19, -8, 16, 16, 159, 18)		// #27 - Drill Frame 1(Walk Frame 2)
	SpriteFrame(-15, -7, 16, 16, 159, 18)		// #28 - Drill Frame 1(Walk Frame 3)
	SpriteFrame(-19, -8, 16, 16, 159, 18)		// #29 - Drill Frame 1(Walk Frame 4)
	SpriteFrame(-19, -8, 16, 16, 159, 18)		// #30 - Drill Frame 1(Walk Frame 4Dub)
	SpriteFrame(-22, -7, 16, 16, 176, 18)		// #31 - Drill Frame 2(Walk Frame 1)
	SpriteFrame(-19, -8, 16, 16, 176, 18)		// #32 - Drill Frame 2(Walk Frame 2)
	SpriteFrame(-15, -7, 16, 16, 176, 18)		// #33 - Drill Frame 2(Walk Frame 3)
	SpriteFrame(-19, -8, 16, 16, 176, 18)		// #34 - Drill Frame 2(Walk Frame 4)
	SpriteFrame(-19, -8, 16, 16, 176, 18)		// #35 - Drill Frame 2(Walk Frame 4Dub)
	SpriteFrame(-6, -7, 16, 16, 193, 1)			// #36 - Drill Base(Walk Frame 1)
	SpriteFrame(-3, -8, 16, 16, 193, 1)			// #37 - Drill Base(Walk Frame 2)
	SpriteFrame(1, -7, 16, 16, 193, 1)			// #38 - Drill Base(Walk Frame 3)
	SpriteFrame(-3, -8, 16, 16, 193, 1)			// #39 - Drill Base(Walk Frame 4)
	SpriteFrame(-3, -8, 16, 16, 193, 1)			// #40 - Drill Base(Walk Frame 4Dub)
	SpriteFrame(-16, -20, 32, 40, 60, 42)		// #41 - Walk Frame 1
	SpriteFrame(-16, -20, 32, 40, 93, 42)		// #42 - Walk Frame 2
	SpriteFrame(-16, -20, 32, 40, 126, 42)		// #43 - Walk Frame 3
	SpriteFrame(-16, -20, 32, 40, 93, 42)		// #44 - Walk Frame 4
	SpriteFrame(-16, -20, 32, 40, 93, 42)		// #45 - Walk Frame 4Dub
	
	// Used to be below LoadSpriteSheet, moved here for consistency
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Drill Bot]
			Object[ArrayPos0].XOriginPos = Object[ArrayPos0].XPos
			Object[ArrayPos0].YOriginPos = Object[ArrayPos0].YPos
		end if
		ArrayPos0++
	loop
end sub
// ========================
// Editor Subs
// ========================

sub RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
		case 0 // Sprite
			checkResult = object.propertyValue
			break
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
		case 0 // Sprite
			object.propertyValue = editor.variableValue
			break
		end switch
	end if
end sub

sub RSDKDraw
	if object.propertyValue == 0
		DrawSprite(0)
		DrawSprite(1)
	else
		DrawSprite(2)
		DrawSprite(3)
	end if
end sub

sub RSDKLoad
	LoadSpriteSheet("R2/Objects.gif")
	SpriteFrame(-8, -36, 16, 16, 26, 1)			// #0 - Drill Frame 1
	SpriteFrame(-12, -20, 24, 40, 1, 1)			// #1 - IDLE

	SpriteFrame(-8, -36, 16, 16, 26, 18)		// #23 - Drill Frame 1
	SpriteFrame(-12, -20, 24, 40, 1, 42)		// #25 - IDLE

	SetVariableAlias(ALIAS_VAR_PROPVAL, "condition")

	AddEditorVariable("condition")
	SetActiveVariable("condition")

	AddEnumVariable("Good", 0)
	AddEnumVariable("Bad", 1)
end sub